/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduler.view.dialogs;

import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import scheduler.core.Processor;
import scheduler.core.task.Taskj;
import scheduler.core.task.VirtualTaskPool;
import scheduler.view.windows.ResultsWindowController;
import scheduler.view.windows.TaskCreatorController;
import scheduler.view.windows.VirtualPoolController;

/**
 *
 * @author Dima
 */
public class DialogUtils {
    public static Taskj showTaskCreator(int numOfPriors) throws IOException {        
        FXMLLoader loader = new FXMLLoader(TaskCreatorController.class.getResource("TaskCreator.fxml"));
        TaskCreatorController.setNumOfPriors(numOfPriors);
        Pane myPane = (Pane) loader.load();
        Scene scene = new Scene(myPane, myPane.getPrefWidth(), myPane.getPrefHeight());
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Нова задача");
        stage.setScene(scene);
        stage.showAndWait();
        
        Taskj task = loader.<TaskCreatorController>getController().getTask();
        
        return task;
    }
    
    public static Taskj showTaskCreator(Taskj task, int numOfPriors) throws IOException {        
        FXMLLoader loader = new FXMLLoader(TaskCreatorController.class.getResource("TaskCreator.fxml"));
        Pane myPane = (Pane) loader.load();
        TaskCreatorController.setNumOfPriors(numOfPriors);
        loader.<TaskCreatorController>getController().setValues(task);
        Scene scene = new Scene(myPane, myPane.getPrefWidth(), myPane.getPrefHeight());
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Редагування даних");
        stage.setScene(scene);
        stage.showAndWait();
                
        Taskj editedTask = loader.<TaskCreatorController>getController().getTask();
        
        return editedTask;
    }
    
    public static VirtualTaskPool showPoolCreator(int num) throws IOException {        
        FXMLLoader loader = new FXMLLoader(VirtualPoolController.class.getResource("VirtualPool.fxml"));
        VirtualPoolController.setNextPrior(num);
        Pane myPane = (Pane) loader.load();
        Scene scene = new Scene(myPane, myPane.getPrefWidth(), myPane.getPrefHeight());
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Нова черга задач");
        stage.setScene(scene);
        stage.showAndWait();
            
        VirtualTaskPool task = loader.<VirtualPoolController>getController().getVirtualPool();
        
        return task;
    }
    
    public static VirtualTaskPool showPoolCreator(VirtualTaskPool task, int num) throws IOException {        
        FXMLLoader loader = new FXMLLoader(VirtualPoolController.class.getResource("VirtualPool.fxml"));
        Pane myPane = (Pane) loader.load();
        loader.<VirtualPoolController>getController().setValues(task, num);
        Scene scene = new Scene(myPane, myPane.getPrefWidth(), myPane.getPrefHeight());
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Редагування данних");
        stage.setScene(scene);
        stage.showAndWait();
                
        VirtualTaskPool editedTask = loader.<VirtualPoolController>getController().getVirtualPool();
        
        return editedTask;
    }
    
    public static void showErrorDialog(String message) throws IOException {
        FXMLLoader loader = new FXMLLoader(ErrorDialogController.class.getResource("ErrorDialog.fxml"));
        Pane myPane = (Pane) loader.load();
        Scene scene = new Scene(myPane, myPane.getPrefWidth(), myPane.getPrefHeight());
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Помилка!");
        stage.setScene(scene);
        loader.<ErrorDialogController>getController().setMessage(message);
        stage.showAndWait();
    }
    
    public static void showResultsWindow(Processor processor) throws IOException {
        FXMLLoader loader = new FXMLLoader(ResultsWindowController.class.getResource("ResultsWindow.fxml"));
        Pane myPane = (Pane) loader.load();
        Scene scene = new Scene(myPane, myPane.getPrefWidth(), myPane.getPrefHeight());
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("Вихідні дані");
        stage.setScene(scene);
        loader.<ResultsWindowController>getController().setProcessor(processor);
        stage.showAndWait();
    }
}
